package fr.isen.web.webservicetemp.controller;

import fr.isen.web.webservicetemp.Service.ApiService;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 *
 * @author louis madeuf
 */
@Component("asyncWorker")
@Scope("singleton")
public class AsynchronousWorker {
    
        
    @Async
    public void bourrageAsynchrone(ApiService service) throws InterruptedException{
        
        System.out.println("--------------------------------------------------------- BOURRAGE 1 DEBUT -----------------------------------------------------");
                
        //WARNING : janvier = 0, fevrier = 1, etc...
        
        //API LIMITS : 10 calls per minute and 500 per day
        Thread.sleep(12000); //== pause beetween all 5 calls allowed of storeWeather method.

        //bourrage 1 (congis)
        Calendar d1 = Calendar.getInstance();
        int realMonthValue = 5;
        d1.set(2010, realMonthValue, 15);
        service.storeWeather(d1.getTime(), "FR/Congis");  //== 2 API calls
        
//        //API LIMIT
//        Thread.sleep(12000);
//
//        //bourrage 2 (lille)
//        Calendar d2 = Calendar.getInstance();
//        realMonthValue = 1;
//        d2.set(2009, realMonthValue, 5);
//        service.storeWeather(d2.getTime(), "FR/Lille");
//        
//        //API LIMIT
//        Thread.sleep(12000);
//
//        //bourrage 3 (marseille)
//        Calendar d3 = Calendar.getInstance();
//        realMonthValue = 8;
//        d3.set(2007, realMonthValue - 1, 10);
//        service.storeWeather(d3.getTime(), "FR/Marseille");
        
        System.out.println("--------------------------------------------------------- BOURRAGE 1 FIN -----------------------------------------------------");
        
     }
    
    @Async
    public void bourrageAsynchrone2(ApiService service) throws InterruptedException{
        
        // BALAYAGE FRANCE !!!
        
        int realMonthValue;
        Calendar d = Calendar.getInstance();
        List<String> towns = new ArrayList();
        
        System.out.println("--------------------------------------------------------- BOURRAGE 2 DEBUT -----------------------------------------------------");
        
       /* //bourrage : ligne verticale traversant la France sous Lille
        towns = service.buildTownList((float)3.0, (float)43.0, (float)50.6, (float)0.9, true);
        realMonthValue = 2;
        d.set(2010, realMonthValue - 1, 20);
        service.storeWeatherList(d.getTime(), towns);
        
        //bourrage : ligne horizontale traversant la France depuis Crozon
        towns.clear();
        towns = service.buildTownList((float)48.246, (float)-4.489, (float)7.5, (float)0.9, false);
        realMonthValue = 3;
        d.set(2010, realMonthValue - 1, 20);
        service.storeWeatherList(d.getTime(), towns);*/
        
        //bourrage : ligne horizontale traversant la France depuis Lorient
        /*towns.clear();
        towns = service.buildTownList((float)47.748, (float)-3.370, (float)7.5, (float)0.9, false);
        realMonthValue = 1;
        d.set(2008, realMonthValue - 1, 4);
        service.storeWeatherList(d.getTime(), towns);
        
        //bourrage : ligne horizontale traversant la France depuis Nantes
        towns.clear();
        towns = service.buildTownList((float)47.218, (float)-1.553, (float)6.5, (float)0.9, false);
        realMonthValue = 7;
        d.set(2008, realMonthValue - 1, 2);
        service.storeWeatherList(d.getTime(), towns);*/
        
        System.out.println("--------------------------------------------------------- BOURRAGE 2 FIN -----------------------------------------------------");
                
    }
    
    @Async
    public void bourrageAsynchrone3(ApiService service) throws InterruptedException{
        // BOURRAGE FRANCE approximee par un rectangle
        Calendar cal = Calendar.getInstance();
        cal.set(2000, 12, 3);
        service.rectangularCollection((float) -4.60, (float) 42.25, (float) 8.20, (float) 51.20, (float) 1.00, cal.getTime());
    }
    
    @Async
    public void bourrageAsynchrone4(ApiService service) throws InterruptedException{
        // BOURRAGE MONDE
        Calendar cal = Calendar.getInstance();
        cal.set(2000, 10, 3);
        service.rectangularCollection((float) -180.00, (float) -90.00, (float) 180.00, (float) 90.00, (float) 1.00, cal.getTime());        
    }
    
    @Async
    public void bourrageAsynchrone5(ApiService service) throws InterruptedException{
        // BOURRAGE ANNEE
        
        //service.yearCollection(2002, "FR/Paris");
        //service.yearCollection(2005, "FR/Rouen");
        service.yearCollection(1998, "FR/Lille");
        
        System.out.println("-------------------BOURRAGE ANNEE FINI --------------------");
        
    }
    
    @Async
     public void stuffingHist(ApiService service, int year, String city) throws InterruptedException{
        // BOURRAGE ANNEE        
        service.yearCollection(year, city);
        
    }
    
    @Async
     public void stuffingGeo(ApiService service, float originlong,float originlat, float distancelong, float distancelat, float step,Date date) throws InterruptedException{
        // BOURRAGE ANNEE
        service.rectangularCollection(originlong, originlat, originlong+distancelong,originlat+distancelat, step, date);        
        
    }
}
